{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "premium-czech",
   "metadata": {},
   "source": [
    "# Sharing X,Y-axis Scale Limits\n",
    "\n",
    "Use `sharex`, `sharey` parameters in the `gggrid()` function to control sharing of axis scale limits among plots in grid:\n",
    "\n",
    "- \"all\" or `true` - share limits between all subplots\n",
    "- \"none\" or `false` - do not share limits between subplots\n",
    "- \"row\" - share limits between subplots in the same row\n",
    "- \"col\" - share limits between subplots in the same column"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "figured-silly",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"7Rvb1U\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"library\">\n",
       "       if(!window.letsPlotCallQueue) {\n",
       "           window.letsPlotCallQueue = [];\n",
       "       }; \n",
       "       window.letsPlotCall = function(f) {\n",
       "           window.letsPlotCallQueue.push(f);\n",
       "       };\n",
       "       (function() {\n",
       "           var script = document.createElement(\"script\");\n",
       "           script.type = \"text/javascript\";\n",
       "           script.src = \"https://cdn.jsdelivr.net/gh/JetBrains/lets-plot@v4.2.0/js-package/distr/lets-plot.min.js\";\n",
       "           script.onload = function() {\n",
       "               window.letsPlotCall = function(f) {f();};\n",
       "               window.letsPlotCallQueue.forEach(function(f) {f();});\n",
       "               window.letsPlotCallQueue = [];\n",
       "               \n",
       "               \n",
       "           };\n",
       "           script.onerror = function(event) {\n",
       "               window.letsPlotCall = function(f) {};\n",
       "               window.letsPlotCallQueue = [];\n",
       "               var div = document.createElement(\"div\");\n",
       "               div.style.color = 'darkred';\n",
       "               div.textContent = 'Error loading Lets-Plot JS';\n",
       "               document.getElementById(\"7Rvb1U\").appendChild(div);\n",
       "           };\n",
       "           var e = document.getElementById(\"7Rvb1U\");\n",
       "           e.appendChild(script);\n",
       "       })();\n",
       "   </script>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%useLatestDescriptors\n",
    "%use lets-plot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "unlike-samuel",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Lets-Plot Kotlin API v.4.6.0. Frontend: Notebook with dynamically loaded JS. Lets-Plot JS v.4.2.0."
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "LetsPlot.getInfo()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "proper-gross",
   "metadata": {},
   "outputs": [],
   "source": [
    "import java.util.Random\n",
    "\n",
    "val rand = java.util.Random(37)\n",
    "\n",
    "val dat1 = mapOf(\n",
    "    \"x\" to List(1000) { rand.nextGaussian() }\n",
    ")\n",
    "\n",
    "val dat2 = mapOf(\n",
    "    \"x\" to List(200) { rand.nextGaussian() }\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "covered-reduction",
   "metadata": {},
   "outputs": [],
   "source": [
    "val p1 = letsPlot(dat1) { x = \"x\" } + geomHistogram()\n",
    "val p2 = letsPlot(dat2) { x = \"x\" } + geomHistogram()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ceramic-garbage",
   "metadata": {},
   "source": [
    "#### 1. Two Independent Charts in Grid\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "stopped-sixth",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"Vppuny\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"layout\":{\n",
       "\"name\":\"grid\",\n",
       "\"ncol\":2,\n",
       "\"nrow\":1,\n",
       "\"fit\":true,\n",
       "\"align\":false\n",
       "},\n",
       "\"figures\":[{\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"bin\",\n",
       "\"position\":\"stack\",\n",
       "\"geom\":\"histogram\",\n",
       "\"data\":{\n",
       "\"..count..\":[1.0,0.0,1.0,3.0,7.0,8.0,9.0,21.0,20.0,23.0,44.0,54.0,66.0,80.0,71.0,85.0,103.0,76.0,92.0,66.0,51.0,41.0,28.0,22.0,13.0,8.0,3.0,2.0,1.0,1.0],\n",
       "\"x\":[-3.3879569114892867,-3.1663525746568784,-2.9447482378244696,-2.7231439009920613,-2.501539564159653,-2.279935227327244,-2.058330890494836,-1.8367265536624275,-1.615122216830019,-1.3935178799976105,-1.1719135431652021,-0.9503092063327938,-0.728704869500385,-0.5071005326679767,-0.2854961958355684,-0.06389185900315963,0.1577124778292487,0.379316814661657,0.6009211514940658,0.8225254883264741,1.0441298251588824,1.2657341619912907,1.487338498823699,1.7089428356561083,1.9305471724885166,2.152151509320925,2.3737558461533332,2.5953601829857416,2.81696451981815,3.038568856650559]\n",
       "}\n",
       "}],\n",
       "\"mapping\":{\n",
       "\"x\":\"x\"\n",
       "},\n",
       "\"data\":{\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[]\n",
       "},{\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"bin\",\n",
       "\"position\":\"stack\",\n",
       "\"geom\":\"histogram\",\n",
       "\"data\":{\n",
       "\"..count..\":[1.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,1.0,2.0,2.0,5.0,9.0,8.0,9.0,13.0,17.0,22.0,26.0,18.0,20.0,9.0,13.0,5.0,7.0,5.0,4.0,0.0,1.0,2.0],\n",
       "\"x\":[-4.113277264892719,-3.8815765101388413,-3.649875755384963,-3.4181750006310847,-3.1864742458772066,-2.954773491123328,-2.72307273636945,-2.491371981615572,-2.259671226861694,-2.0279704721078153,-1.7962697173539373,-1.5645689626000592,-1.3328682078461807,-1.1011674530923026,-0.8694666983384245,-0.637765943584546,-0.4060651888306679,-0.1743644340767898,0.057336320677088715,0.2890370754309668,0.5207378301848449,0.752438584938723,0.984139339692601,1.21584009444648,1.447540849200358,1.6792416039542362,1.9109423587081142,2.1426431134619923,2.3743438682158704,2.6060446229697494]\n",
       "}\n",
       "}],\n",
       "\"mapping\":{\n",
       "\"x\":\"x\"\n",
       "},\n",
       "\"data\":{\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[]\n",
       "}],\n",
       "\"kind\":\"subplots\"\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"Vppuny\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();    \n",
       "   </script>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gggrid(listOf(p1, p2))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dangerous-master",
   "metadata": {},
   "source": [
    "#### 2. Share Scale Limits of the Y-axis"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "efficient-doubt",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"l0xh7v\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"layout\":{\n",
       "\"name\":\"grid\",\n",
       "\"ncol\":2,\n",
       "\"nrow\":1,\n",
       "\"sharey\":\"all\",\n",
       "\"fit\":true,\n",
       "\"align\":false\n",
       "},\n",
       "\"figures\":[{\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"bin\",\n",
       "\"position\":\"stack\",\n",
       "\"geom\":\"histogram\",\n",
       "\"data\":{\n",
       "\"..count..\":[1.0,0.0,1.0,3.0,7.0,8.0,9.0,21.0,20.0,23.0,44.0,54.0,66.0,80.0,71.0,85.0,103.0,76.0,92.0,66.0,51.0,41.0,28.0,22.0,13.0,8.0,3.0,2.0,1.0,1.0],\n",
       "\"x\":[-3.3879569114892867,-3.1663525746568784,-2.9447482378244696,-2.7231439009920613,-2.501539564159653,-2.279935227327244,-2.058330890494836,-1.8367265536624275,-1.615122216830019,-1.3935178799976105,-1.1719135431652021,-0.9503092063327938,-0.728704869500385,-0.5071005326679767,-0.2854961958355684,-0.06389185900315963,0.1577124778292487,0.379316814661657,0.6009211514940658,0.8225254883264741,1.0441298251588824,1.2657341619912907,1.487338498823699,1.7089428356561083,1.9305471724885166,2.152151509320925,2.3737558461533332,2.5953601829857416,2.81696451981815,3.038568856650559]\n",
       "}\n",
       "}],\n",
       "\"mapping\":{\n",
       "\"x\":\"x\"\n",
       "},\n",
       "\"data\":{\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[]\n",
       "},{\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"bin\",\n",
       "\"position\":\"stack\",\n",
       "\"geom\":\"histogram\",\n",
       "\"data\":{\n",
       "\"..count..\":[1.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,1.0,2.0,2.0,5.0,9.0,8.0,9.0,13.0,17.0,22.0,26.0,18.0,20.0,9.0,13.0,5.0,7.0,5.0,4.0,0.0,1.0,2.0],\n",
       "\"x\":[-4.113277264892719,-3.8815765101388413,-3.649875755384963,-3.4181750006310847,-3.1864742458772066,-2.954773491123328,-2.72307273636945,-2.491371981615572,-2.259671226861694,-2.0279704721078153,-1.7962697173539373,-1.5645689626000592,-1.3328682078461807,-1.1011674530923026,-0.8694666983384245,-0.637765943584546,-0.4060651888306679,-0.1743644340767898,0.057336320677088715,0.2890370754309668,0.5207378301848449,0.752438584938723,0.984139339692601,1.21584009444648,1.447540849200358,1.6792416039542362,1.9109423587081142,2.1426431134619923,2.3743438682158704,2.6060446229697494]\n",
       "}\n",
       "}],\n",
       "\"mapping\":{\n",
       "\"x\":\"x\"\n",
       "},\n",
       "\"data\":{\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[]\n",
       "}],\n",
       "\"kind\":\"subplots\"\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"l0xh7v\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();    \n",
       "   </script>"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gggrid(listOf(p1, p2), sharey = true)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "informed-triumph",
   "metadata": {},
   "source": [
    "#### 3. Share Scale Limits of both, X and Y-axis"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "surgical-clone",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"Ctuo4b\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"layout\":{\n",
       "\"name\":\"grid\",\n",
       "\"ncol\":2,\n",
       "\"nrow\":1,\n",
       "\"sharex\":\"all\",\n",
       "\"sharey\":\"all\",\n",
       "\"fit\":true,\n",
       "\"align\":false\n",
       "},\n",
       "\"figures\":[{\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"bin\",\n",
       "\"position\":\"stack\",\n",
       "\"geom\":\"histogram\",\n",
       "\"data\":{\n",
       "\"..count..\":[1.0,0.0,1.0,3.0,7.0,8.0,9.0,21.0,20.0,23.0,44.0,54.0,66.0,80.0,71.0,85.0,103.0,76.0,92.0,66.0,51.0,41.0,28.0,22.0,13.0,8.0,3.0,2.0,1.0,1.0],\n",
       "\"x\":[-3.3879569114892867,-3.1663525746568784,-2.9447482378244696,-2.7231439009920613,-2.501539564159653,-2.279935227327244,-2.058330890494836,-1.8367265536624275,-1.615122216830019,-1.3935178799976105,-1.1719135431652021,-0.9503092063327938,-0.728704869500385,-0.5071005326679767,-0.2854961958355684,-0.06389185900315963,0.1577124778292487,0.379316814661657,0.6009211514940658,0.8225254883264741,1.0441298251588824,1.2657341619912907,1.487338498823699,1.7089428356561083,1.9305471724885166,2.152151509320925,2.3737558461533332,2.5953601829857416,2.81696451981815,3.038568856650559]\n",
       "}\n",
       "}],\n",
       "\"mapping\":{\n",
       "\"x\":\"x\"\n",
       "},\n",
       "\"data\":{\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[]\n",
       "},{\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"bin\",\n",
       "\"position\":\"stack\",\n",
       "\"geom\":\"histogram\",\n",
       "\"data\":{\n",
       "\"..count..\":[1.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,1.0,2.0,2.0,5.0,9.0,8.0,9.0,13.0,17.0,22.0,26.0,18.0,20.0,9.0,13.0,5.0,7.0,5.0,4.0,0.0,1.0,2.0],\n",
       "\"x\":[-4.113277264892719,-3.8815765101388413,-3.649875755384963,-3.4181750006310847,-3.1864742458772066,-2.954773491123328,-2.72307273636945,-2.491371981615572,-2.259671226861694,-2.0279704721078153,-1.7962697173539373,-1.5645689626000592,-1.3328682078461807,-1.1011674530923026,-0.8694666983384245,-0.637765943584546,-0.4060651888306679,-0.1743644340767898,0.057336320677088715,0.2890370754309668,0.5207378301848449,0.752438584938723,0.984139339692601,1.21584009444648,1.447540849200358,1.6792416039542362,1.9109423587081142,2.1426431134619923,2.3743438682158704,2.6060446229697494]\n",
       "}\n",
       "}],\n",
       "\"mapping\":{\n",
       "\"x\":\"x\"\n",
       "},\n",
       "\"data\":{\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[]\n",
       "}],\n",
       "\"kind\":\"subplots\"\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"Ctuo4b\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();    \n",
       "   </script>"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gggrid(listOf(p1, p2), sharex = true, sharey = true)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Kotlin",
   "language": "kotlin",
   "name": "kotlin"
  },
  "language_info": {
   "codemirror_mode": "text/x-kotlin",
   "file_extension": ".kt",
   "mimetype": "text/x-kotlin",
   "name": "kotlin",
   "nbconvert_exporter": "",
   "pygments_lexer": "kotlin",
   "version": "1.8.20"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
